/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;


import me.zhengjie.dto.sqlServer.OutBoundMission;
import me.zhengjie.repository.OutBoundMissionRepository;
import me.zhengjie.service.OutBoundMissionService;
import me.zhengjie.service.dto.OutBoundMissionDto;
import me.zhengjie.service.dto.OutBoundMissionQueryCriteria;
import me.zhengjie.service.mapper.OutBoundMissionMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-13
**/
@Service
@RequiredArgsConstructor
public class OutBoundMissionServiceImpl implements OutBoundMissionService {

    private final OutBoundMissionRepository outBoundMissionRepository;
    private final OutBoundMissionMapper outBoundMissionMapper;

    @Override
    public PageResult<OutBoundMissionDto> queryAll(OutBoundMissionQueryCriteria criteria, Pageable pageable){
        Page<OutBoundMission> page = outBoundMissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(outBoundMissionMapper::toDto));
    }

    @Override
    public List<OutBoundMissionDto> queryAll(OutBoundMissionQueryCriteria criteria){
        return outBoundMissionMapper.toDto(outBoundMissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OutBoundMissionDto findById(Integer id) {
        OutBoundMission outBoundMission = outBoundMissionRepository.findById(id).orElseGet(OutBoundMission::new);
        ValidationUtil.isNull(outBoundMission.getId(),"OutBoundMission","id",id);
        return outBoundMissionMapper.toDto(outBoundMission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(OutBoundMission resources) {
        outBoundMissionRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OutBoundMission resources) {
        OutBoundMission outBoundMission = outBoundMissionRepository.findById(resources.getId()).orElseGet(OutBoundMission::new);
        ValidationUtil.isNull( outBoundMission.getId(),"OutBoundMission","id",resources.getId());
        outBoundMission.copy(resources);
        outBoundMissionRepository.save(outBoundMission);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            outBoundMissionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OutBoundMissionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OutBoundMissionDto outBoundMission : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" missionId",  outBoundMission.getMissionCode());
            map.put(" missionStatus",  outBoundMission.getMissionStatus());
            map.put(" palletCode",  outBoundMission.getPalletCode());
            map.put(" source",  outBoundMission.getSource());
            map.put(" destination",  outBoundMission.getDestination());
            map.put(" createTime",  outBoundMission.getCreateTime());
            map.put(" startTime",  outBoundMission.getStartTime());
            map.put(" finishTime",  outBoundMission.getFinishTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int findLastMissionCount(int orderId) {
        return outBoundMissionRepository.findCountByOrderId(orderId);
    }

    @Override
    public List<OutBoundMission> findOutBoundMissionByOrderId(Integer orderId) {
        return outBoundMissionRepository.findOutBoundMissionsByOutBoundOrderId(orderId);
    }
}